﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace WpfApplication1
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        //日历控件
        Months month = new Months();
        //日期是否被选中
        public bool IsSelectedDay = false;

        //控件集合
        List<Canvas> CanvasList = new List<Canvas>(8);
        List<Label> LabelList = new List<Label>(8);
        List<TextBox> TextBoxList = new List<TextBox>(8);
       
        //动画StoryBoard
        Storyboard TextFadeOut = new Storyboard();
        Storyboard TextFadeIn = new Storyboard();
        Storyboard canvas2StoryDown = new Storyboard();
        Storyboard canvas2StoryUp = new Storyboard();

        public Window1()
        {
            //窗体初始化构造
            InitializeComponent();
            //性能优化
            System.Diagnostics.Process.GetCurrentProcess().MaxWorkingSet = (IntPtr)210000;
            //将表格每一列组成集合，便于遍历
            LabelList.AddRange(new Label[] { label9, label10, label11, label12, label13, label14, label15, label16 });
            CanvasList.AddRange(new Canvas[] { canvas6, canvas7, canvas8, canvas9, canvas10, canvas11, canvas12, canvas13 });
            TextBoxList.AddRange(new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8 });

       #region 初始化动画
            //构造控件的动画效果

            //Textbox淡入淡出动画
            foreach (TextBox t in TextBoxList)
            {
                NameScope.SetNameScope(t, new NameScope());
                this.RegisterName(t.Name, t);
                DoubleAnimation s1 = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(500)));
                Storyboard.SetTargetName(s1, t.Name);
                Storyboard.SetTargetProperty(s1, new PropertyPath(TextBox.OpacityProperty));
                DoubleAnimation s2 = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromMilliseconds(500)));
                Storyboard.SetTargetName(s2, t.Name);
                Storyboard.SetTargetProperty(s2, new PropertyPath(TextBox.OpacityProperty));
                DoubleAnimation s3 = new DoubleAnimation(0, 215, new Duration(TimeSpan.FromMilliseconds(500)));
                Storyboard.SetTargetName(s3, t.Name);
                Storyboard.SetTargetProperty(s3, new PropertyPath(TextBox.WidthProperty));
                DoubleAnimation s4 = new DoubleAnimation(215, 0, new Duration(TimeSpan.FromMilliseconds(500)));
                Storyboard.SetTargetName(s4, t.Name);
                Storyboard.SetTargetProperty(s4, new PropertyPath(TextBox.WidthProperty));
                TextFadeIn.Children.Add(s1);
                TextFadeIn.Children.Add(s3);
                TextFadeOut.Children.Add(s2);
                TextFadeOut.Children.Add(s4);
            }

            //grid3和grid4淡入淡出与滑动动画
            NameScope.SetNameScope(this.grid3, new NameScope());
            this.RegisterName(this.grid3.Name, this.grid3);

            NameScope.SetNameScope(this.grid4, new NameScope());
            this.RegisterName(this.grid4.Name, this.grid4);

            DoubleAnimation onShow = new DoubleAnimation(0.2, 1, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTargetName(onShow, grid3.Name);
            Storyboard.SetTargetProperty(onShow, new PropertyPath(Grid.OpacityProperty));

            DoubleAnimation onHide = new DoubleAnimation( 1,0.2, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTargetName(onHide, grid3.Name);
            Storyboard.SetTargetProperty(onHide, new PropertyPath(Grid.OpacityProperty));

            DoubleAnimation onC2Down = new DoubleAnimation(0, 315, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTargetName(onC2Down, grid4.Name);
            Storyboard.SetTargetProperty(onC2Down, new PropertyPath(Grid.HeightProperty));

            DoubleAnimation onC2Up = new DoubleAnimation(315, 0, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTargetName(onC2Up, grid4.Name);
            Storyboard.SetTargetProperty(onC2Up, new PropertyPath(Grid.HeightProperty));

            Days.father = this;
            Days.Detail.Children.Add(onShow);
            Days.Brief.Children.Add(onHide);

            canvas2StoryDown.Children.Add(onC2Down);
            canvas2StoryUp.Children.Add(onC2Up);
    #endregion

            //添加控件
            grid2.Children.Add(month);
            
        }

        #region 左右翻动月份
        private void image1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            month.NextMonth();
        }

        private void image2_MouseDown(object sender, MouseButtonEventArgs e)
        {
            month.PreMonth();
        }

        private void image1_MouseEnter(object sender, MouseEventArgs e)
        {
            ImageSourceConverter isc = new ImageSourceConverter();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Properties.Resources.right1.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ((Image)sender).Source = (ImageSource)isc.ConvertFrom(ms);
        }

        private void image1_MouseLeave(object sender, MouseEventArgs e)
        {
            ImageSourceConverter isc = new ImageSourceConverter();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Properties.Resources.right.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ((Image)sender).Source = (ImageSource)isc.ConvertFrom(ms);
        }

        private void image2_MouseEnter(object sender, MouseEventArgs e)
        {
            ImageSourceConverter isc = new ImageSourceConverter();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Properties.Resources.left1.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ((Image)sender).Source = (ImageSource)isc.ConvertFrom(ms);
        }

        private void image2_MouseLeave(object sender, MouseEventArgs e)
        {
            ImageSourceConverter isc = new ImageSourceConverter();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Properties.Resources.left.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ((Image)sender).Source = (ImageSource)isc.ConvertFrom(ms);
        }
        #endregion

        #region 功能按钮点击
        bool IsShow2 = false;
        bool IsShow3 = false;
        public bool IsSelectFirst = false;
        private void Canvas2_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsShow2)
            {
                canvas2StoryDown.Begin(this);
                IsShow2 = !IsShow2;
            }
            else
            {
                canvas2StoryUp.Begin(this);
                IsShow2 = !IsShow2;
            }
        }

        private void Canvas3_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (IsSelectedDay)
            {
                if (!IsShow3)
                {
                    TextFadeIn.Begin(this);
                    IsShow3 = !IsShow3;
                }
                else
                {
                    
                    TextFadeOut.Begin(this);
                    for (int i = 0; i < TextBoxList.Count; i++)
                    {
                        if (TextBoxList[i].Text.Trim() != "")
                        {
                            CourseRecord.AddThings(Days.SelectedDay.Year, Days.SelectedDay.Month, Days.SelectedDay.Day, i, TextBoxList[i].Text);
                        }
                    }
                    updateDayDetails(Days.SelectedDay.Day);
                    IsShow3 = !IsShow3;
                }
            }
        }
        private void Canvas1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            IsSelectFirst = true;
        }
        #endregion


        #region comboBox初始化
        private void comboBoxFrom_Initialized(object sender, EventArgs e)
        {
            for (int i = 1; i < 50; i++)
            {
                comboBoxFrom.Items.Add(i.ToString());
            }
        }

        private void comboBoxTo_Initialized(object sender, EventArgs e)
        {
            for (int i = 1; i < 50; i++)
            {
                comboBoxTo.Items.Add(i.ToString());
            }
        }
        #endregion

        #region grid4功能按钮
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            listBox1.Items.Add(new CourseItem(Convert.ToInt32(comboBoxFrom.Text), Convert.ToInt32(comboBoxTo.Text),  comboBoxTime.SelectedIndex,(comboBoxWeek.SelectedIndex + 1) % 7, textBoxAdd.Text));
            grid4ClearDownPart();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            CourseRecord cr = new CourseRecord(textBoxName.Text, textBoxTeacher.Text, textBoxCredit.Text);
            foreach (CourseItem ci in listBox1.Items)
            {
                cr.AddRecord(ci.From, ci.To, ci.Week, ci.Time, ci.Address);
            }
            grid4ClearUpPart();
            grid4ClearDownPart();
        }
        private void grid4ClearUpPart()
        {
            textBoxName.Clear();
            textBoxTeacher.Clear();
            textBoxCredit.Clear();
            listBox1.Items.Clear();
        }
        private void grid4ClearDownPart()
        {
            comboBoxFrom.Text = "";
            comboBoxTo.Text = "";
            comboBoxWeek.SelectedIndex = 0;
            comboBoxTime.SelectedIndex = 0;
            textBoxAdd.Clear();
            
        }
        #endregion

        #region grid3方法集合
        //更新所有当前信息
        public void updateDayDetails(int day)
        {
            for(int i=0;i<8;i++)
            {
                RecordItem temp=CourseRecord.getDetails(Months.now.Year, Months.now.Month, day, i);
                if (temp.CourseName != "")
                {
                    LabelList[i].Content = temp.CourseName + " @" + temp.CourseAddress;
                }
                else
                {
                    LabelList[i].Content = temp.ThingsContent;
                }
                 ImageSourceConverter isc = new ImageSourceConverter();

                 if (temp.LinkList.Count == 1)
                 {
                         System.IO.MemoryStream ms = new System.IO.MemoryStream();
                         FileIconPick.GetFileIcon(temp.LinkList[0].path).ToBitmap().Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                         CanvasList[i].Background = new ImageBrush((ImageSource)isc.ConvertFrom(ms));
                         CanvasList[i].ToolTip = temp.LinkList[0];
                 }
                 else if (temp.LinkList.Count > 1)
                 {
                     System.IO.MemoryStream ms = new System.IO.MemoryStream();
                     Properties.Resources.muti.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                     CanvasList[i].Background = new ImageBrush((ImageSource)isc.ConvertFrom(ms));
                     CanvasList[i].ToolTip = temp.LinkList;
                 }
                 else
                 {
                     System.IO.MemoryStream ms = new System.IO.MemoryStream();
                     Properties.Resources.blank.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                     CanvasList[i].Background = new ImageBrush((ImageSource)isc.ConvertFrom(ms));
                     CanvasList[i].ToolTip = "把文件拖进来吧~~";
                 }
            }
        }
        //文件拖拽功能
        private void canvas6_DragEnter(object sender, DragEventArgs e)
        {
            
        }

        private void canvas6_Drop(object sender, DragEventArgs e)
        {
            if (IsSelectedDay)
            {
                String[] filenames = (String[])e.Data.GetData(DataFormats.FileDrop);
                foreach (String filename in filenames)
                {
                    String[] temp = filename.Split('.');
                    String extra = temp[temp.Length - 1];
                    if (temp.Length == 1)
                    { extra = ""; }
                    CourseRecord.AddLink(Days.SelectedDay.Year, Days.SelectedDay.Month, Days.SelectedDay.Day, Convert.ToInt32(((Canvas)sender).Uid), extra, filename);
                }
                updateDayDetails(Days.SelectedDay.Day);
            }
        }
        //点击运行程序
        private void canvas13_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(((Canvas)sender).ToolTip.ToString());
            }
            catch
            {
            }
        }

        #endregion

        private void label9_MouseEnter(object sender, MouseEventArgs e)
        {
            CourseRecord cr = CourseRecord.getCourseDetail(((Label)sender).Content.ToString().Split('@')[0].Trim());
            if(cr.name!="")
                ((Label)sender).ToolTip = cr.name + "\n" + cr.teacher + "\n" + cr.credit;
        }

        
    }
}
